package com.mousycoder.mysql.dao;

import com.mousycoder.mysql.domain.User;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * TODO
 *
 * @author mousycoder
 * @version 1.0
 * @date 2022/3/22 8:40 PM
 */
@Repository
public class UserDaoImpl implements UserDao {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public Boolean add(User user) {
        String sql = "INSERT INTO user(id,name) values(?,?)";
        return jdbcTemplate.update(sql,user.getId(),user.getName()) > 1;
    }

    @Override
    public Boolean update(User user) {
        return null;
    }

    @Override
    public User locate(Long id) {
        String sql = "SELECT * FROM user WHERE ID = ?";
        SqlRowSet sqlRowSet = jdbcTemplate.queryForRowSet(sql, id);
        if (sqlRowSet.next()){
            return generateEntity(sqlRowSet);
        }
        return null;
    }

    private User generateEntity(SqlRowSet rs){
        User user = new User();
        user.setId(rs.getLong("id"));
        user.setName(rs.getString("name"));
        return user;
    }


    @Override
    public List<User> matchName(String name) {
        return null;
    }
}
